package bite20220320;

import variable.Array;

import java.util.Arrays;

public class ArrayTest {
    public static void main(String[] args) {
        int[] array = {1,2,3,4};
        int[] copy = new int[array.length];
        copy = array.clone();
    }
    public static void main10(String[] args) {
        int[] array = {1,2,3,4};
        int[] copy = new int[array.length];
        System.arraycopy(array,0,copy,0,array.length);
    }
    public static void main9(String[] args) {
        int[] arr = {1,2,3,4};
        int[] arr1 = Arrays.copyOf(arr,arr.length);
    }
    public static void main8(String[] args) {
        //for循环拷贝
        int[] array = {1,2,3,4};
        int[] copy = new int[array.length];

        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }
    }








    public static String toString(int[] arr){
        String str = "[";
        for (int i = 0; i < arr.length; i++) {
            str = str + arr[i];
            if (i < arr.length-1){
                str = str+",";
            }
        }
        str = str+"]";
        return str;
    }

    public static void main7(String[] args) {
        int[] arr = {2,3,4,5};
        System.out.println(toString(arr));
    }


    public static int[]  fun(int n){
        int[] arr = new int[5];
        for (int i = 0; i < n; i++) {
            arr[i] = i;
        }
        return arr;
    }
    public static void main6(String[] args) {
        int[] arr = fun(5);
        System.out.println(Arrays.toString(arr));
    }

    public static void  func3(int[] arr){
        arr[0] = 199;
    }
    public static void func2(int[] arr){
        arr = new int[]{11,12,13,14};
    }
    public static void main5(String[] args) {
        int[] array1 = {1,2,3,4};
        func2(array1);
        System.out.println(Arrays.toString(array1));
        //对象有一个地址，引用只是保存这个地址的变量

        int[] array2 = {100,99,98,77};
        func3(array2);
        System.out.println(Arrays.toString(array2));
    }
    public static void main4(String[] args) {
        int[] arr1 = {1,2,3,4};
        int[] arr2 = arr1;//两个引用指向同一个对象
        System.out.println("array1:"+Arrays.toString(arr1));
        System.out.println("array2:"+Arrays.toString(arr2));

        System.out.println("=====================");
        arr1[0] = 99;
        System.out.println("array2:"+Arrays.toString(arr2));

    }
    public static void main3(String[] args) {
        long[] array = new long[3];//局部变量,java虚拟机栈//引用变量，引用了一个数组对象
        //引用类型初始化为null//数组元素的初始化值为0
        System.out.println(Arrays.toString(array));

    }
    public static void main2(String[] args) {
        int[] arr = {1,2,3};
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        //for each
        for (int x:arr){
            System.out.print(x+" ");
        }

        //借助Arrays
        System.out.println(Arrays.toString(arr));//把参数的数组转化为字符串输出
    }
    public static void main1(String[] args) {
        int[] arr = new int[10];
        System.out.println(arr[0]);

        int[] array;
        array = new int[]{1,2,3,4,5};
        array = new int[]{9,4,5,7,7,83,};

        int[] array2 = {1,2,3,4,5,6};
        array2 = new int[]{1,2,3,4,5,6};//静态初始化只有一次机会
        System.out.println(Arrays.toString(array2));
    }
}
